package com.hackathon.sgstern.overview;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.model.LatLng;
import com.hackathon.sgstern.R;
import com.hackathon.sgstern.SurroundingArea;
import com.hackathon.sgstern.dataprovider.LogoProvider;
import com.hackathon.sgstern.model.SportEvent;

public class SportEventListAdapter extends BaseAdapter {

	public List<SportEvent> sportEvents;
	public List<Float> distances;
	LayoutInflater inflator;
	Context context;

	private static final SimpleDateFormat format = new SimpleDateFormat(
			"dd.MM.yyyy HH:mm", Locale.GERMANY);

	private static final DecimalFormat numberFormat = (DecimalFormat) DecimalFormat
			.getInstance(Locale.GERMANY);

	public SportEventListAdapter(Context context, List<SportEvent> sportEvents) {
		inflator = LayoutInflater.from(context);
		this.context = context;
		this.sportEvents = sportEvents;
	}

	@Override
	public int getCount() {
		return sportEvents.size();
	}

	@Override
	public Object getItem(int position) {
		return sportEvents.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SportEvent sport = sportEvents.get(position);
		convertView = inflator.inflate(R.layout.list_item_sportevent, null);
		TextView title = (TextView) convertView
				.findViewById(R.id.list_sportevent_title);
		TextView date = (TextView) convertView
				.findViewById(R.id.list_sportevent_date);
		TextView participants = (TextView) convertView
				.findViewById(R.id.list_sportevent_participants);
		TextView city = (TextView) convertView
				.findViewById(R.id.list_sportevent_city);
		TextView distance = (TextView) convertView
				.findViewById(R.id.list_sportevent_distance);
		ImageView image = (ImageView) convertView
				.findViewById(R.id.list_sportevent_icon);
		Bitmap bitmap = LogoProvider.retrieveBitmap(context, sport.art);
		image.setImageBitmap(bitmap);
		title.setText(sport.title);
		date.setText(format.format(sport.time));
		participants.setText(sport.participants.size() + "/"
				+ sport.maxTeilnehmer);
		city.setText(getCityOfPoint(sport.getOrt()));

		if (distances != null) {
			numberFormat.setMaximumFractionDigits(2);
			String distanceString = numberFormat.format(distances.get(position)
					.doubleValue() / 1000) + " km";
			distance.setText(distanceString);
		}
		return convertView;
	}

	private String getCityOfPoint(LatLng location) {
		String city = "";
		try {
			Geocoder geoCoder = new Geocoder(context, Locale.GERMANY);
			List<Address> adresses = geoCoder.getFromLocation(
					location.latitude, location.longitude, 1);
			if (!adresses.isEmpty()) {
				Address address = adresses.get(0);
				city = address.getLocality();
			}
		} catch (IOException ex) {
		}
		return city;
	}
}
